package com.netease.ssm.controller;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.netease.ssm.cons.CommonConstant;
import com.netease.ssm.meta.Content;
import com.netease.ssm.meta.Trx;
import com.netease.ssm.meta.TrxProduct;
import com.netease.ssm.meta.User;
import com.netease.ssm.service.ProductService;
import com.netease.ssm.service.ShowService;
import com.netease.ssm.service.UserService;

@Controller
public class AccountController extends BaseController {
	@Autowired
	private ProductService productService;
	@Autowired
	private ShowService showService;

	@RequestMapping(value = "/account", method = RequestMethod.GET)
	public ModelAndView showAllTrx(HttpServletRequest request,HttpServletResponse respone) {
		ModelAndView mav = new ModelAndView();
		User user = (User) request.getSession().getAttribute(CommonConstant.USER_CONTEXT);
		if(user != null  && user.getUserType()==0 ){
			List<Trx> trxList = productService.getAllTrx();
			List<TrxProduct> buyList = new ArrayList<TrxProduct>();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
			for (Trx trx : trxList) {
				Content content = new Content();
				content = showService.getProductById(trx.getContentid());
				TrxProduct trxProduct = new TrxProduct();
				trxProduct.setId(trx.getContentid());
				trxProduct.setBuyPrice(trx.getPrice());
				trxProduct.setBuyTime(sdf.format(trx.getTime()));
				if (content != null) {
					trxProduct.setTitle(content.getTitle());
					try {
						trxProduct.setImage(new String(content.getImage(),"UTF-8"));
					} catch (UnsupportedEncodingException e) {
						e.printStackTrace();
					}
				}
				buyList.add(trxProduct);
			}
			mav.setViewName("account");
			mav.addObject("buyList", buyList);
		}else{
			try {
				respone.sendRedirect("/");
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return mav;
	}

	@Autowired
	private UserService userService;

	@RequestMapping(value = "/api/buy", params = "id", method = RequestMethod.POST)
	public ModelAndView buyProduct(HttpServletRequest request, @RequestParam(value = "id") int id, User user) {
		user = (User) request.getSession().getAttribute(CommonConstant.USER_CONTEXT);
		User dbUser = userService.getPersonByUserName(user.getUserName());
		Content content = showService.getProductById(id);
		Date d = new Date();
		Long time = d.getTime();
		Trx trx = new Trx();
		trx.setPersonid(dbUser.getId());
		trx.setContentid(id);
		trx.setPrice(content.getPrice());
		trx.setTime(time);
		productService.buyProduct(trx);
		ModelAndView mav = new ModelAndView();
		if (dbUser != null && content != null) {
			mav.addObject("code", 200);
			mav.addObject("message", "success");
			mav.addObject("result", true);
		} else {
			mav.addObject("code", 401);
			mav.addObject("message", "faile");
			mav.addObject("result", false);
		}
		return mav;

	}

}
